local skel = fk.CreateSkill {
  name = "lb__zhengdi",
}

Fk:loadTranslationTable {
  ["lb__zhengdi"] = "征镝",
  [":lb__zhengdi"] = "你可以将本轮不同于上次发动〖征镝〗选择的区域的所有牌当作目标至多为选择牌数的【万箭齐发】使用，若你的攻击范围等于目标数，你摸一张牌，本轮你的攻击范围及〖征镝〗摸牌数+1。",

  ["#lb__zhengdi_use"] = "征镝",
  ["#lb__zhengdi"] = "征镝：你可以将一个区域的所有牌当作至多等量目标的【万箭齐发】使用",
  ["@lb__zhengdi-round"] = "征镝",

  ["$lb__zhengdi1"] = "我将，巡征追猎。",
  ["$lb__zhengdi2"] = "翾翔不坠，万载常胜！",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  prompt = "#lb__zhengdi",
  pattern = "archery_attack",
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".",
  },
  interaction = function(self, player)
    local all = { "$Hand", "$Equip", "$Judge" }
    local ask = {}
    for i, area in ipairs(all) do
      if #player:getCardIds(i) > 0 and area ~= player:getMark("lb__zhengdi-round") then
        table.insert(ask, area)
      end
    end
    if #ask == 0 then return nil end
    return UI.ComboBox {
      choices = ask,
      all_choices = all,
    }
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    return nil
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    if self.interaction.data == nil then return end
    local all = { "$Hand", "$Equip", "$Judge" }
    local ids = player:getCardIds(table.indexOf(all, self.interaction.data))
    local clone = Fk:cloneCard("archery_attack")
    clone.skillName = skel.name
    clone:addSubcards(ids)
    return #ids > 0 and #selected < #ids and player:canUseTo(clone, to_select) and clone.skill:modTargetFilter(player, to_select, selected, clone)
  end,
  on_use = function(self, room, cardUseEvent, _, params)
    local player = cardUseEvent.from
    local tos = cardUseEvent.tos
    room:sortByAction(tos)
    if self.interaction.data == nil then return end
    room:setPlayerMark(player, "lb__zhengdi-round", self.interaction.data)
    local all = { "$Hand", "$Equip", "$Judge" }
    local ids = player:getCardIds(table.indexOf(all, self.interaction.data))
    local clone = Fk:cloneCard("archery_attack")
    clone.skillName = skel.name
    clone:addSubcards(ids)
    return {  ---@type UseCardDataSpec
      from = player,
      tos = tos,
      card = clone,
    }
  end,
  feasible = function(self, player, selected, selected_cards, card)
    if self.interaction.data == nil then return end
    local all = { "$Hand", "$Equip", "$Judge" }
    local ids = player:getCardIds(table.indexOf(all, self.interaction.data))
    return #ids > 0 and #selected <= #ids and #selected > 0
  end,
  enabled_at_play = function(self, player)
    for _, s in ipairs({ "h", "e", "j" }) do
      if #player:getCardIds(s) > 0 then return true end
    end
    return false
  end,
  enabled_at_response = function(self, player, response)
    if response then return end
    for _, s in ipairs({ "h", "e", "j" }) do
      if #player:getCardIds(s) > 0 then return true end
    end
    return false
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  --由于PreCardUse在牌移动前触发，摸牌需要在下一个时机触发，否则被转化的武器会计入距离计算
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return data.card.skillName == "lb__zhengdi" and target == player and #data.tos == player:getAttackRange()
  end,
  on_use = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@lb__zhengdi-round")
    player:drawCards(player:getMark("@lb__zhengdi-round"), "lb__zhengdi")
  end,
})

skel:addEffect("atkrange", {
  correct_func = function(self, from, to)
    if from:hasSkill("lb__zhengdi") then
      return from:getMark("@lb__zhengdi-round")
    end
  end,
})

return skel
